package com.jin.webvideo.service;

import com.jin.webvideo.base.util.CollectionsUtil;
import com.jin.webvideo.entity.Video;
import com.jin.webvideo.entity.VideoInfo;
import com.jin.webvideo.externalInterfaces.entity.Filmography;
import com.jin.webvideo.mapper.VideoMapper;
import com.jin.webvideo.util.base.URLUtil;
import com.jin.webvideo.util.entity.EntityUtil;
import com.jin.webvideo.util.base.MapBuilder;
import com.jin.webvideo.util.base.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VideoService {
    @Autowired
    private VideoMapper videoMapper;

    /**
     * 获取封面地址列表
     * @param platform
     * @param docid
     * @param tvid
     * @return
     */
    public List<String> getCover(String platform, String docid, String tvid) {
        List<String> info = videoMapper.selectCover(docid, tvid, platform);
        if (!CollectionsUtil.isEmpty(info)){
            return info;
        }
        if ("defaultVid".equals(tvid)) {
            return null;
        }
        return getCover(platform, docid, "defaultVid");
    }

    /**
     * 获取当前播放进度
     * @param account
     * @param pkey
     * @return null 未登录， 0，不存在记录
     */
    public Long getCurrentProgress(String account, String pkey){
        if (StringUtil.isEmpty(account) || StringUtil.isEmpty(pkey)){
            return null;
        }
        Long currentTime = videoMapper.getCurrentTime(account, pkey);
        if (null == currentTime){
            return 0L;
        }
        return currentTime;
    }

    /**
     * 更新/新增播放进度
     * @param account
     * @param pkey
     * @param time
     * @return
     */
    public boolean upsertProgress(String account, String pkey, long time) {
        Long currentTime = videoMapper.getCurrentTime(account, pkey);
        if (null == currentTime){
            return videoMapper.insertProgress(account, pkey, time) == 1;
        }
        return videoMapper.updateProgress(account, pkey, time) == 1;
    }

    /**
     * 新增 filmography 到数据库
     * @param filmography
     */
    public void addVideoInfo(Filmography filmography){
        List<VideoInfo> infos = EntityUtil.toListInfo(filmography);
        List<String> existList = videoMapper.findVidInEpList(filmography.getPlatform(),filmography.getId() ,infos);
        // 过滤已存在的数据
        infos = CollectionsUtil.filter(infos, (episodeInfo) -> !existList.contains(episodeInfo.getVid()));
        // 插入数据
        if (!CollectionsUtil.isEmpty(infos)){
            videoMapper.insertVideoInfo(infos);
        }
    }

    /**
     * 新增 filmographys 到数据库
     * @param filmographys
     */
    public void addFilmographys(List<Filmography> filmographys){
        List<Video> videos = CollectionsUtil.convert(filmographys, (accept) -> EntityUtil.toVideo(accept) );
        insertVideo(videos);
    }

    /**
     * 新增 videos 到数据库
     * @param videos
     */
    public void insertVideo(List<Video> videos){
        if (CollectionsUtil.isEmpty(videos)){
            return;
        }
        List<String> existvideos = videoMapper.findVideosInList(videos);
        // video 去重
        CollectionsUtil.distinct(videos, (v) -> format(v.getDocid(), v.getPlatform()));
        // 过滤已存在的数据
        videos = CollectionsUtil.filter(videos, (video) -> !existvideos.contains(format(video.getDocid(), video.getPlatform())));
        if (!CollectionsUtil.isEmpty(videos)){
            videoMapper.insertVideo(videos);
        }
    }

    /**
     * 查找用户的播放记录
     * @param account
     * @param pageNo
     * @param pageSize
     * @return
     */
    public List<Map<String,String>> listVideoHistory(String account, int pageNo, int pageSize){
        List<Map<String,String>> infos = videoMapper.userVideoHis(account,(pageNo-1)*pageSize, pageSize + 1);
        return infos;
    }

    /**
     * 获取跳过的视频片段
     * @param url
     * @param account
     * @return
     */
    public Map<String, Integer> findSegInfo(String url, String account){
        url = URLUtil.urlNoProtocol(url);
        VideoInfo info = videoMapper.findVideoInfoByUrl(url);
        if (null == info || StringUtil.isEmpty(account)){
            Map<String, Integer> segInfo = new HashMap<>();
            segInfo.put("beginSeg", 0);
            segInfo.put("beginSegLen", 0);
            segInfo.put("endSeg", 0);
            segInfo.put("endSegLen", 0);
            return segInfo;
        }
        return findSegInfo(info.getPlatform(), info.getDocid(), info.getVid());
    }

    /**
     * 获取跳过的视频片段
     * @param platform
     * @param docid
     * @param tvid
     * @return
     */
    public Map<String, Integer> findSegInfo(String platform, String docid, String tvid){
        Map<String,Integer> segInfo = videoMapper.findSegInfo(platform, docid, tvid);
        if (null == segInfo){
            segInfo = videoMapper.findSegInfo(platform, docid, "defaultVid");
        }
        if (null == segInfo){
            MapBuilder<String, Integer> builder = new MapBuilder<>();
            segInfo = builder.put("beginSeg", 0).put("beginSegLen", 0)
                    .put("endSeg", 0).put("endSegLen", 0).build();
        }
        return segInfo;
    }
    public String format(String docid, String platform){
        return String.format("%s\u001d%s", docid, platform);
    }
}
